<?php

header("Content-type: text/html; charset=utf-8");
// 授权回调地址，将用户的UID与授权返回码存入
/*1、连接数据库 
        端口号或者地址 
        登录数据库的登录名
        登录密码
        要操作的数据库名
        */
$link = mysqli_connect('127.0.0.1', 'wrwer', 'werwer', 'werwer', '3306');
if (!$link) {
    $error = mysqli_error();
    echo "<script>alert('{$error}')</script>";;
    die;
}
//2、选择要操作的数据库
mysqli_select_db($link, 'ewrw');
//3、设置交互编码
mysqli_query($link, 'set names utf8');


// 从链接取出uid 授权码
// $get=[
//         'uid'=>'%3C?php%20echo%20(222);%20?%3E',
//         'app_auth_code'=>'511dc4c494f04ddaba8f4a36297f3X12',
// ];


// $str=urldecode($_GET['uid']);
// $str=substr($str, -8);

if (!$_POST) {

    // print_r($_GET);
    $uid = $_GET['uid'];
    $app_auth_code = $_GET['app_auth_code'];
    // $app_auth_code='356945609';

    $sql = "select `real_status` from `jd_kbxx` where `uid` ={$uid} limit 1";
    $one = mysqli_query($link, $sql);
    $status = mysqli_fetch_assoc($one);
// print_r($status);die;
    if ($status['real_status'] == 0) {
        echo "<script>alert('该商户未经平台授权，无法入驻！如有疑问请联系客服处理。')</script>";
        die;
    }
    $sql = "insert into `jd_kbsh` (`uid`,`app_auth_code`) values ({$uid},'{$app_auth_code}')";
    $id = mysqli_query($link, $sql);
// echo $sql;   echo $id;die;
    if ($id > 0) {
        echo '<form id="form" action="" method="post" style="width:90%;margin:auto;margin-top:5%; font-size:40px;color:#00AAEF;">
        <p style="height:60px;line-height:60px;margin-bottom:5%;">请填入您的信息：</p>
        <label style="width:25%;height:60px;line-height:60px;text-align:right;display: inline-block;margin-bottom:5%;">姓名：</label><input id="name" style="width:60%;height:60px;line-height:60px;font-size:40px;border:1px solid #00AAEF" type="text" name="name" value="" /><br /> 
        <label style="width:25%;height:60px;line-height:60px;text-align:right;display: inline-block;margin-bottom:5%;">支付宝：</label><input id="alipay" style="width:60%;height:60px;line-height:60px;font-size:40px;border:1px solid #00AAEF" type="text" name="alipay" value="" /><br />
        <p style="height:40px;">请选择收款范围：</p>
        <style>  
            .label{margin:20px 20px 0 0;display:block}  
            .radio{display:none}  
            .radioInput{background-color:#fff;border:2px solid rgba(0,0,0,0.15);border-radius:100%;display:inline-block;height:36px;margin-right:10px;margin-top:-1px;vertical-align:middle;width:36px;line-height:1}  
            .radio:checked + .radioInput:after{background-color:#00A1E9;border-radius:100%;content:"";display:inline-block;height:26px;margin-left:5px;margin-top:5px;width:26px}  
            .checkbox.radioInput,.radio:checked + .checkbox.radioInput:after{border-radius:0}  
            .radio:checked + .radioInput{border:2px solid #00A1E9;}  
        </style>
        <label class="label"><input type="radio" class="radio" checked name="type" value="1"><span class="radioInput"></span>1-500 </label>
        <label class="label"><input type="radio" class="radio" name="type" value="2"><span class="radioInput"></span>501-800 </label>
        <label class="label"><input type="radio" class="radio" name="type" value="3"><span class="radioInput"></span>801-2000 </label>
        <label class="label"><input type="radio" class="radio" name="type" value="4"><span class="radioInput"></span>2001-10000 </label>
        <input  type="hidden" name="code" id="code" value="' . $app_auth_code . '" />
        <input style="width:25%;height:60px;line-height:60px;background:#fff;border:1px solid #00AAEF;color:#00AAEF;font-size:40px;margin-top:20px;" type="button" class="button" onclick="fun()" value="确定"/>
  </form>';

        echo "<script> function fun(){
                          var name=document.getElementById('name').value
                          var alipay=document.getElementById('alipay').value
                          var code=document.getElementById('code').value
                          if(isNull(name)||isNull(alipay)){
                            alert('请务必填写您的信息')
                          }else{
                            var form=document.getElementById('form');
                            form.submit(); 
                          }
                        }

                        function isNull(arg1)
                        {
                         return !arg1 && arg1!==0 && typeof arg1!=='boolean'?true:false;
                        }
          </script>";
    } else {
        echo "<script>alert('授权失败，请稍后重试')</script>";
    }


} else {
    $name = $_POST['name'];
    $alipay = $_POST['alipay'];
    $code = $_POST['code']; //app_auth_code
    $type = $_POST['type'];
    // print_r($_POST);die;
    if (empty($name) || empty($alipay) || empty($code)) {

    } else {
        $sql = "update `jd_kbsh` set `name`='{$name}',`alipay`='{$alipay}',`type`='{$type}' where `app_auth_code`='{$code}'";
        $id = mysqli_query($link, $sql);
        if ($id > 0) {
            echo "<script>alert('授权申请成功，请耐心等待审核，请勿多次授权！')</script>";
        } else {
            // $sql   ="delete from `jd_kbsh` where `app_auth_code`='{$code}'";
            // $id    =mysqli_query($link,$sql);
            echo "<script>alert('授权失败，请稍后重试')</script>";
        }
    }


}

die;






// http://weixin.jcsmkj.cn/pay.php?app_id=2017060107400865&source=alipay_app_auth&uid=%3C?php%20echo%20();%20?%3E&app_auth_code=511dc4c494f04ddaba8f4a36297f3X12
// http://weixin.jcsmkj.cn/pay.php?app_id=2017060107400865&source=alipay_app_auth&app_auth_code=506ab940fa894aa39af8649ea8f0aF12
// $myfile = fopen("app_auth_code.txt", "w") or die("Unable to open file!");


// $app_auth_code=$_GET['uid'];
// fwrite($myfile, $app_auth_code);
// fclose($myfile);
/*

$str="uid=<?php echo (222); ?>";
$test=substr($str, -8);
$test=substr($test, 0,-5);
print_r($test);die;
*/ 






